package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.RoleDao;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class RoleServiceImpl implements RoleService {
    @Override
    public void save(Role role) {

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取Dao
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //id使用UUID的生成策略来获取
        String id = UUID.randomUUID().toString();
        role.setId(id);
        //3.调用Dao层操作
        roleDao.save(role);


    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取Dao
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        for (String id : ids) {
            roleDao.deleteRolePermission(id);
            roleDao.delete(id);
        }
        //3.调用Dao层操作

    }

    @Override
    public void update(Role role) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取Dao
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //3.调用Dao层操作
        roleDao.update(role);

    }

    @Override
    public Role findById(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取Dao
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //3.调用Dao层操作

        return roleDao.findById(id);

    }

    @Override
    public List<Role> findAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取Dao
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //3.调用Dao层操作

        return roleDao.findAll();

    }

    @Override
    public PageInfo findAll(int page, int size) {

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取Dao
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //3.调用Dao层操作
        PageHelper.startPage(page, size);
        List<Role> all = roleDao.findAll();
        PageInfo pageInfo = new PageInfo(all);

        return pageInfo;
    }


    @Override
    public void updateRolePermission(String roleId, String permissionIds) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        roleDao.deleteRolePermission(roleId);
        String[] permissionArray = permissionIds.split(",");

        if (permissionArray.length >0) {
            for (String permissionId : permissionArray) {
                RoleDao roleDao1 = sqlSession.getMapper(RoleDao.class);
                roleDao1.saveRoleModule(roleId, permissionId);
            }
        }
            sqlSession.close();
    }

    @Override
    public List<Role> findAllRoleByUserId(String userId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取Dao
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //3.调用Dao层操作
        return roleDao.findAllRoleByUserId(userId);
    }

    @Override
    public PageInfo findByName(int page, int size,String name) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        PageHelper.startPage(page, size);
        List<Role> all = roleDao.findByName(name);

        PageInfo<Role> pageInfoAndName = new PageInfo<Role>(all);

        return pageInfoAndName;

    }

}